package com.heima.kafka.simple;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;

/*消息生产者*/
public class ProducerFastStart {
    public static final String TOPIC = "topic";
    public static void main(String[] args) {
        /*添加kafka的配置信息*/
        Properties properties = new Properties();
        /*封装消息*/
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.200.151:9092");
        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
        properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
        //生产者对象
        KafkaProducer<String, String> kafkaProducer = new KafkaProducer<>(properties);
        /*发送消息*/
        ProducerRecord<String, String> record = new ProducerRecord<String, String>(TOPIC,"001","hello kafka!");
        try {
            kafkaProducer.send(record);
        } catch (Exception e) {
            e.printStackTrace();
        }
        /*关闭消息通道*/
        kafkaProducer.close();
    }
}
